<template>
    <div class="app-container">
        <el-row :gutter="20">
            <!--设备数据-->
            <el-col :span="24" :xs="24">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                    label-width="68px">
                    <el-form-item label="设备编码" prop="deviceSerial">
                        <el-input v-model="queryParams.deviceSerial" placeholder="请输入设备编码" clearable
                            style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item label="设备名称" prop="deviceName">
                        <el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable
                            style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>

                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                            v-hasPermi="['mes:dv:machinery:add']">新增</el-button>
                    </el-col>
                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>

                <el-table v-loading="loading" :data="machineryList" border >
                    <el-table-column label="设备编码"  align="center" key="deviceSerial" prop="deviceSerial">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" @click="handleView(scope.row)"
                                v-hasPermi="['mes:dv:machinery:query']">{{ scope.row.deviceSerial }}</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="设备名称"  align="center" key="deviceName" prop="deviceName"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="备注"  align="center" key="remark" prop="remark"/>
                    <el-table-column label="创建时间" align="center" prop="createTime">
                        <template slot-scope="scope">
                            <span>{{ parseTime(scope.row.createTime) }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                                v-hasPermi="['mes:dv:machinery:edit']">修改</el-button>
                            <el-button size="mini" type="text" icon="el-icon-video-play"
                                @click="lookThoroughfare(scope.row)"
                                v-hasPermi="['sale:purchase:edit']">查看通道</el-button>
                            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                                v-hasPermi="['mes:dv:machinery:remove']">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageSize"
                    :limit.sync="queryParams.pageSize" @pagination="getList" />
            </el-col>
        </el-row>

        <!-- 添加或修改设备对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="设备编码" prop="itemCode">
                            <el-input v-model="form.deviceSerial" readonly="readonly" maxlength="64"
                                v-if="optType == 'view'" />
                            <el-input v-model="form.deviceSerial" placeholder="请输入设备编码" maxlength="64" v-else />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="设备名称" prop="itemName">
                            <el-input v-model="form.deviceName" maxlength="255" readonly="readonly"
                                v-if="optType == 'view'" />
                            <el-input v-model="form.deviceName" placeholder="请输入设备名称" maxlength="255" v-else />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="备注">
                            <el-input v-model="form.remark" type="textarea" readonly
                                v-if="optType == 'view'"></el-input>
                            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" v-else></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="cancel" v-if="optType == 'view'">返回</el-button>
                <el-button type="primary" @click="submitForm" v-else>确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import { listCamera, getCamera, delCamera, addCamera, updateCamera } from "@/api/monitor/workShop";
export default {
    name: "workshopMonitor",
    data() {
        return {
            optType: undefined,
            // 遮罩层
            loading: true,
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 物料产品表格数据
            machineryList: [],
            // 弹出层标题
            title: "",
            // 设备类型树选项
            machineryTypeOptions: [],
            //车间选项
            workshopOptions: [],
            // 是否显示弹出层
            open: false,
            // 设备类型名称
            machineryTypeName: undefined,
            // 表单参数
            form: {},
            defaultProps: {
                children: "children",
                label: "machineryTypeName"
            },
            // 查询参数
            queryParams: {
                page: 0,
                pageSize: 10,
                deviceSerial: null,
                deviceName: null,
            },

            // 表单校验
            rules: {
                deviceSerial: [
                    { required: true, message: "设备编码不能为空", trigger: "blur" },
                    { max: 64, message: '设备编码长度必须小于64个字符', trigger: 'blur' }
                ],
                deviceName: [
                    { required: true, message: "设备名称不能为空", trigger: "blur" }
                ],
            }
        };
    },
    watch: {
        // 根据设备分类名称筛选分类树
        machineryTypeName(val) {
            this.$refs.tree.filter(val);
        }
    },
    created() {
        this.getList();
    },
    methods: {

        /** 查询列表 */
        async getList() {
            // 使用axios发送get请求
            this.loading = true;
            listCamera(this.queryParams).then(response => {
                let data = response.data
                this.machineryList = data.content.map(item=>item.camera);
                this.total = data.totalElements;
                this.loading = false;
            }
            );
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                deviceSerial: null,
                deviceName: null,
                remark: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageSize = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 查询明细按钮操作
        handleView(row) {
            this.reset();
            const id= row.id
            getCamera(id).then(response => {
                this.form = response.data.camera;
                this.open = true;
                this.title = "查看设备信息";
                this.optType = "view";
            });
        },
        /** 新增按钮操作 */
        handleAdd() {
            debugger;
            this.reset();
            this.optType = "add";
            this.open = true;
            this.title = "新增设备";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id
            getCamera(id).then(response => {
                this.form = response.data.camera;
                this.open = true;
                this.title = "修改设备";
                this.optType = "edit";
            });
        },
        /** 查看通道操作 */
        lookThoroughfare(row) {
            const deviceSerial = row.deviceSerial
            this.$router.push({ path: '/workshop/thoroughfare', query: { deviceSerial } })
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != undefined) {
                        updateCamera(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        debugger;
                        addCamera(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const id = row.id;
            this.$modal.confirm('确认删除数据项？').then(function () {
                return delCamera(id);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
    }
};
</script>